--[[

	ClosetGnome_OhNoes patching:
	
		Prevents unequipping of weapons if the user is not a Warrior / Rogue and solo.
		Prevents spamming user with "Couldn't find BLAH in your inventory." if OhNoes fails (which never happens, right? RIGHT?!).


	TODO:
		Optional integration with oRA / CT_RA to disable aggro-swapping if you are a tank (good for those DPSing Ferals).
		                          KLH Threat Meter / Threat-1.0 to disable aggro-swapping if you are a tank (Druid in Bear form, Paladin or Warrior - rage or class == WARRIOR).
		Optional setup for BigWigs and OhNoes => different damage thresholds for different bossfights. Not very useful, but hey...

]]--

local myLib = {};
SarfFix2.ClosetGnome_OhNoes = myLib;

myLib.setup = {};
myLib.setup.patchFuncs = {
	"UNIT_HEALTH";
	"Banzai_PlayerGainedAggro";
};

myLib.setup.options = {
	disableWhenSolo = true;
	disableWhenSoloClasses = {
		["ROGUE"] = true;
		["WARRIOR"] = true;
	};
};

myLib.options = myLib.setup.options;

function myLib:PatchFuncs()
	local oldFunc = nil;
	local newFunc = nil;
	if ( not myLib.setup.oldFuncs ) then
		myLib.setup.oldFuncs = {};
	end
	if ( type(AceLibrary) == nil ) then
		return;
	end
	local ClosetGnome_OhNoes = AceLibrary("ClosetGnomeOhNoes");
	for k, v in pairs(myLib.setup.patchFuncs) do
		oldFunc = ClosetGnome_OhNoes[v];
		if ( type(oldFunc) == "function" ) then
			myLib.setup.oldFuncs[v] = oldFunc;
			newFunc = function(lib, p1,p2,p3,p4,p5,p6,p7,p8,p9,p10) if not myLib:OhNoesOK() then return; else return oldFunc(lib,p1,p2,p3,p4,p5,p6,p7,p8,p9,p10) end end;
			ClosetGnomeOhNoes[v] = newFunc;
		end
	end
end

function myLib:FixFailedOhNoesSpam()
	local funcName = "EquipSlot";
	local ClosetGnome = AceLibrary("ClosetGnome");
	local oldFunc = ClosetGnome[funcName];
	if ( type(oldFunc) == "function" ) and ( type(ClosetGnome.ItemNameFromSlot) == "function" ) then
		myLib.setup.oldFuncs[funcName] = oldFunc;
		ClosetGnome[funcName] = myLib.EquipSlot;
	else
		myLib:Warn("Could not patch ClosetGnome:EquipSlot - missing required functions, probably due to version mismatch");
	end
end

function myLib:Warn(message)
	ChatFrame1:AddMessage(message, 1, 1, 1);
end

-- Patch for ClosetGnome:EquipSlot, preventing it to do anything if it already has the item equipped.
function myLib.EquipSlot(lib, slot, item, secondTry, p1, p2, p3)
	if ( item ) then
		local itemInSlot = lib:ItemNameFromSlot(slot);
		if ( itemInSlot == item ) then
			return;
		end
	end
	return myLib.setup.oldFuncs["EquipSlot"](lib, slot, item, secondTry, p1, p2, p3);
end

-- Checks if it is OK to do an OhNoes
function myLib:OhNoesOK()
	if ( ( myLib.options.disableWhenSolo ) and ( not UnitInRaid("player") ) and ( not UnitExists("party1") ) ) then
		local class = myLib.setup.playerClass;
		if ( not class ) then
			_, class = UnitClass("player");
			myLib.setup.playerClass = class;
		end
		if ( ( not myLib.options.disableWhenSoloClasses ) or ( myLib.options.disableWhenSoloClasses[class] ) ) then
			return false;
		end
	end
	return true;
end

function SarfFix2:ClosetGnomeOhNoes_Initialize()
	myLib:PatchFuncs();
	myLib:FixFailedOhNoesSpam();
end

function SarfFix2_ComboSounds_ComboSoundCheck(lib)
	if ( UnitPowerType("player") ~= 3 ) then
		return false;
	end
	return SarfFix2_ComboSounds_ComboSoundCheck_Original(lib);
end

if ( ComboSounds ) then
	SarfFix2_ComboSounds_ComboSoundCheck_Original = ComboSounds.ComboSoundCheck;
	ComboSounds.ComboSoundCheck = SarfFix2_ComboSounds_ComboSoundCheck;
end

SarfFix2:PatchAddon("ClosetGnome_OhNoes", SarfFix2.ClosetGnomeOhNoes_Initialize);

